package 动态规划;

/**
 * @description: 给定两个字符串str1和str2，输出连个字符串的最长公共子序列。如过最长公共子序列为空，则输出-1。
 * @author: ywk
 * @date: 2020-11-08
 */
public class 最长公共子序列 {


    public static void main(String[] args) {
        System.out.println(dp("4312", "32"));
        System.out.println(dp("24801235", "1235"));
        System.out.println(LongestBackTrack("123012351", "0212351", 0, 0, ""));

    }

    //1-回溯法
    public static String LongestBackTrack(String str1, String str, int start1, int start2, String temp) {
        String max = "";
        for (int i = start1; i < str1.length(); i++) {
            char c = str1.charAt(i);
            int index = str.indexOf(c, start2);
            if (index == -1) {
                continue;
            }
            if (temp.length() + 1 > max.length()) {
                max = temp + c;
            }
            String s = LongestBackTrack(str1, str, i + 1, index + 1, temp + c);
            if (s.length() > max.length()) {
                max = s;
            }
        }
        return max;
    }

    //2.动态规划
    public static int dp(String s, String t) {
        //sLength和tLength分别是两个字符串的长度
        int sLength = s.length();
        int tLength = t.length();
        int[][] dp = new int[tLength + 1][sLength + 1];

        for (int i = 1; i <= tLength; i++) {
            for (int j = 1; j <= sLength; j++) {
                //下面是递推公式
                if (t.charAt(i - 1) == s.charAt(j - 1)) {
                    //如果字符串t的第i个字符和s的第j个字符一样，
                    //那么有两种选择
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }
        return dp[tLength][sLength];
    }

    static int longestCommonSubStr(String str1, String str2, int i, int j) {
        if (i == -1) {
            return 0;
        }
        if (j == -1) {
            return 0;
        }
        if (str1.charAt(i) == str2.charAt(j)) {
            return longestCommonSubStr(str1, str2, i - 1, j - 1) + 1;
        }
        return Math.max(longestCommonSubStr(str1, str2, i - 1, j), longestCommonSubStr(str1, str2, i, j - 1));
    }


    static String longestCommonSubStrMemo(String str1, String str2) {
        String[][] memo = new String[str1.length() + 1][str2.length() + 1];
        for (int i = 0; i <= str1.length(); i++) {
            memo[i][0] = "";
        }
        for (int i = 0; i <= str2.length(); i++) {
            memo[0][i] = "";
        }
        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    memo[i][j] = memo[i - 1][j - 1] + str1.charAt(i - 1);
                } else {
                    memo[i][j] = memo[i][j - 1].length() > memo[i - 1][j].length() ? memo[i][j - 1] : memo[i - 1][j];
                }
            }
        }
        return memo[str1.length()][str2.length()].equals("") ? "-1" : memo[str1.length()][str2.length()];
    }

}
